// pages/balance/index/index.js
const app = getApp()
var call = require("../../../utils/request.js");
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageStatus: false,
    seeStatus: true, // 保密状态
    moneyData:{}, // 余额次数数据
    offset: 0,// 页码
    limit: 10, // 每页条数
    total: 0, // 数据总条数
    consumptionList:{},// 消费明细
    sumData: {},
    status: false, // 是否还有数据
    tips: true, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.balance(); // 获取余额
    this.consumptionList(this.data.offset,this.data.limit); // 消费明细
  },
  // 数据保密
  seeNo () {
    this.setData({ seeStatus: false })
  },
  // 数据可见
  seeYes () {
    this.setData({ seeStatus: true })
  },
  //获取余额
  balance () {
    call.getData('/api/user/balance', {}, res => {
      if(res.code == 1){
        this.setData({ moneyData: res.data })
      }else{
        wx.showToast({ icon: 'none',title: res.msg })
      }
      this.setData({ pageStatus: true })
    }, err => {
      console.log(err)
    })
  },
  getDataT(){
    this.setData({ consumptionList: {} });
    this.balance(); // 获取余额
    call.getData('/api/user/detailed', { offset: this.data.offset,limit: this.data.limit }, res => {
      if(res.code == 1){
        var rows = res.data.rows;
        var consumptionList = this.data.consumptionList;
        if(rows.length == 0){
          this.setData({ tips: false })
        }
        for(let key  in rows){
          if(key in consumptionList){
            for(let item of rows[key]){
              consumptionList[key].push(item);
            }
          }else{
            consumptionList[key] = rows[key];
          }
          this.setData({
              consumptionList: consumptionList,
              sumData: res.data.sumData,
              total: res.data.total,
          })
      }
      }else{
        wx.showToast({ icon: 'none',title: res.msg })
      }
    }, err => {
      console.log(err)
    })
  },
  //消费明细
  consumptionList (offset,limit) {
    call.getData('/api/user/detailed', { offset,limit }, res => {
      if(res.code == 1){
        var rows = res.data.rows;
        var consumptionList = this.data.consumptionList;
        if(rows.length == 0){
          this.setData({ tips: false })
        }
        for(let key  in rows){
          if(key in consumptionList){
            for(let item of rows[key]){
              consumptionList[key].push(item);
            }
          }else{
            consumptionList[key] = rows[key];
          }
          this.setData({
              consumptionList: consumptionList,
              sumData: res.data.sumData,
              total: res.data.total,
          })
      }


      }else{
        wx.showToast({ icon: 'none',title: res.msg })
      }
    }, err => {
      console.log(err)
    })
  },

  // 查看收费规则
  seeRule () {
    console.log(1)
    wx.navigateTo({
      url: '/pages/balance/clearingRules/index'
    })
  },
  // 微信充值
  recharge () {
    wx.navigateTo({
      url: '/pages/balance/recharge/index'
    })
  },
  // 充值卡充值
  card () {
    wx.navigateTo({
      url: '/pages/balance/card/index'
    })
  },
  
  /**
   * 页面上拉触底事件的处理函数，与点击加载更多做同样的操作
   */
  onReachBottom () {
    let total = this.data.total;  // 总条数
    let offset = this.data.offset; // 偏移量
    let limit = this.data.limit; // 当前页显示条数
    this.setData({ offset: offset + 1 })
    offset = (offset + 1 ) * limit;
    console.log(offset)
    if(total > offset){
      console.log(1)
    }else{
      this.setData({ status: true })
    }
   
    if(this.data.status){
      console.log('没有数据了')
    }else{
      wx.showLoading({
        title: '玩命加载中...',
      });
      this.consumptionList(offset, limit);
      wx.hideLoading();
    }
  },
})